<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/11/21
 * Time: 13:59
 */
namespace app\api\controller;
require_once BASE_ROOT."core/application/api/controller/BaseController.php";
use think\Request;
use think\Db;
use app\api\service\IndexService;
use app\common\model\Config;
use think\Validate;
use think\Cache;
class Index extends BaseController
{
 private $mem_path=array(
     'fenxiao'=>'/yb_shop/pages/fenxiao/pages/index/index?title=分销中心',
     'follow'=>'/yb_shop/pages/member/favorite/index',
     'order'=>'/yb_shop/pages/order/index',
     'coupon'=>'/yb_shop/pages/member/coupon/index',
     'book'=>'/yb_shop/pages/appointment/index',
     'kjm'=>'/yb_shop/pages/kanjia/my_record/index',
     'kjo'=>'/yb_shop/pages/kanjia/order/index',
     'ptm'=>'/yb_shop/pages/pintuan/pages/group/index',
     'pto'=>'/yb_shop/pages/pintuan/pages/orders/index',
     'mso'=>'/yb_shop/pages/miaosha/order/index',
     'address'=>'/yb_shop/pages/member/address/index',
     'about'=>'/yb_shop/pages/member/about/index',
     'paycontent'=>'/yb_shop/pages/paycontent/my/index',
     'kefu'=>'',
     'jfshop'=>'/yb_shop/pages/member/integral/shop',
     'card'=>'/yb_shop/pages/usercard/index/index'
 );
 public function ttt(){
  //echo 1222;
  return  $app_id = Request::instance()->param('i');
 }
 public function goodsMod(){
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id = $this->getMchId($app_id);
  $num = Request::instance()->param('num');
  $sort = Request::instance()->param('sort');
  $cate = Request::instance()->param('cate');
  $data['mch_id']=$mch_id;
  $data['is_del']=0;
  if($cate){
   $data['cate_id'] = ['in',self::Infinitecate($cate,$mch_id)];
  }
  if($sort=='pop'){
   $sort='is_hot';
  }elseif($sort=='sales'){
   $sort='real_sales';
  }else{
   $sort='create_time';
  }
  $order  = new IndexService();
  $info = $order->goodsMod($data,$num,$sort);
  $rs['info']=$info;
  return json_encode($rs);
 }
 public function articleMod(){
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id = $this->getMchId($app_id);
  $num = Request::instance()->param('num');
  $sort = Request::instance()->param('sort');
  $cate = Request::instance()->param('cate');
  $data['mch_id']=$mch_id;
  $data['type']=1;
  if($cate){
   $data['cate_id'] = ['in',self::Infinitecate($cate,$mch_id,'ybsc_article_class','class_id')];
  }
  if($sort=='pop'){
   $sort='click';
  }elseif($sort=='sales'){
   $sort='is_recommend';
  }else{
   $sort='create_time';
  }
  $order  = new IndexService();
  $info = $order->articleMod($data,$num,$sort);
  $rs['info']=$info;
  return json_encode($rs);
 }
 public static function Infinitecate($pid,$mch_id,$table='ybsc_goods_cate',$feild="cate_id",$all=[],$res="")
 {
  $where=['pid'=>$pid,"mch_id"=>$mch_id];
  if($table=='ybsc_goods_cate'){
   $where['is_visible']=1;
  }elseif($table=='ybsc_article_class'){
   $where['is_del']=1;
  }
  $res.=$pid.",";
  $cate_list = Db::name($table)->where($where)->field($feild)->select();
  foreach($cate_list as $key=>$value){
   if(in_array($value[$feild],$all)){
    continue;
   }else {
    array_push($all,$value);
    $s=self::Infinitecate($value[$feild], $mch_id,$all).",";
    if(strlen($s)>1) {
     $res .=$s;
    }
   }
  }
  return substr($res,0,strlen($res)-1);
 }
 public function WriteBook(){
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id = $this->getMchId($app_id);
  $rule = [
      ['mch_id','require','不存在商户'],
      ['name','require'],
      ['phone','require'],
   //['booktime','require'],
      ['user_id','require']
  ];
  $data = [
      'name'  => Request::instance()->param('name'),
      'phone'  => Request::instance()->param('phone'),
   // 'booktime'  => Request::instance()->param('booktime'),
      'content'  => Request::instance()->param('content'),
      'mch_id'=>$mch_id,
      'user_id'=>Request::instance()->param('user_id'),
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $order  = new IndexService();
  $info = $order->WriteBook($data);
  if(empty($info))
  {
   $rs['code'] = 1;
   $rs['msg'] = "提交失败";
   return json_encode($rs);
  }
  if($info==-1){
   $rs['code'] = 1;
   $rs['msg'] = "已添加成功";
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 写评论
  * @return string
  */
 public function WriteComment(){
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id = $this->getMchId($app_id);
  $rule = [
      ['mch_id','require','不存在商户'],
      ['fraction','require'],
      ['info','require'],
      ['user_id','require']
  ];
  $data = [
      'info'  => Request::instance()->param('info'),
      'fraction'  => Request::instance()->param('fraction'),
      'array_pic'  => Request::instance()->post('array_pic/a',[]),
      'mch_id'=>$mch_id,
      'user_id'=>Request::instance()->param('user_id'),
      'del'=>1
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $order  = new IndexService();
  $array_pic='';
  foreach ($data['array_pic'] as $v)
  {
   $array_pic .= $v.",";
  }
  $data['array_pic']=$array_pic;
  $info = $order->WriteComment($data);
  if(empty($info))
  {
   $rs['code'] = 1;
   $rs['msg'] = "提交失败";
   return json_encode($rs);
  }
  if($info==-1){
   $rs['code'] = 1;
   $rs['msg'] = "已添加成功";
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 评论列表
  * @return string
  */
 public function CommentList()
 {
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $num = Request::instance()->param('num',0);
  $page = Request::instance()->param('page',1);
  $mch_id = $this->getMchId($app_id);
  //$page = Request::instance()->param('page',1);
  $rule = [
      ['mch_id','require','不存在商户'],
  ];
  $data = [
      'mch_id'=>$mch_id,
      'del'=>1,
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $order  = new IndexService();
  $info = $order->CommentList($data,$page,$num);
  $info=json_decode($info,true);
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 技术支持二级页面
  * @return string
  */
 public function Support(){
  $rs = array('code' => 0,'info' => array());
  $config=new Config();
  $info=$config->where('key','TECHNICAL')->find();
  if(!empty($info)){
   $value=json_decode($info['value'],true);
   if(!empty($value[0]['qrcode'])){
    $value[0]['qrcode']= IMG_PAH.$value[0]['qrcode'];
   }
   $rs['info']=$value[0];
  }
  return json_encode($rs);
 }
 /**
  * 功能说明：文件(图片)上传
  * @return string
  */
 public function uploadFile()
 {
  $rs = array('code' => 0,'info' => array());
  $app_key = Request::instance()->param('i');       //小程序id
  $path = Request::instance()->param('path');
  $mch_id = $this->getMchId($app_key);
  $file_path=$_SERVER['DOCUMENT_ROOT'].'/attachment/upload/'.$path.'/acid_'.$mch_id.'/'.date('Y-m-d',time()).'/';//
  if ($file_path == "") {
   return null;
//            $rs['code'] = 1;
//            $rs['msg'] = "文件路径不能为空";
//            return json_encode($rs);
  }
  // 检测文件夹是否存在，不存在则创建文件夹
  if (!file_exists($file_path)) {
   $mode = intval('0777', 8);
   mkdir($file_path, $mode, true);
  }
  $file_name = $_FILES["file_upload"]["name"]; // 文件原名
  $file_size = $_FILES["file_upload"]["size"]; // 文件大小
  $file_type = $_FILES["file_upload"]["type"]; // 文件类型
  if ($file_size == 0) {
   return null;
//            $rs['code'] = 1;
//            $rs['msg'] = "文件大小为0MB";
//            return json_encode($rs);
  }
  if ($file_type != "image/gif" && $file_type != "image/png" && $file_type != "image/jpeg" && $file_size > 5000000) {
   return null;
//            $rs['code'] = 1;
//            $rs['msg'] = "文件上传失败,请检查您上传的文件类型,文件大小不能超过5MB";
//            return json_encode($rs);
  }
  $guid = time().rand(10000,99999);
  $file_name_explode = explode(".", $file_name); // 图片名称
  $suffix = count($file_name_explode) - 1;
  $ext = "." . $file_name_explode[$suffix]; // 获取后缀名
  $newfile = $guid . $ext; // 重新命名文件
  $ok = $this->moveUploadFile($_FILES["file_upload"]["tmp_name"], $file_path . $newfile);
  if ($ok["code"])
  {
   // 文件上传成功执行下边的操作
   @unlink($_FILES['file_upload']);
//            $rs['code'] = 0;
//            $rs['info'] = 'http://'.$_SERVER['HTTP_HOST'].strstr($ok["path"],'/attachment');
//            return json_encode($rs);
   return 'https://'.$_SERVER['HTTP_HOST'].strstr($ok["path"],'/attachment');
  }
  else
  {
   return null;
   // 强制将文件后缀改掉，文件流不同会导致上传文件失败
//            $rs['code'] = 1;
//            $rs['msg'] = "请检查您的上传参数配置或上传的文件是否有误";
//            return json_encode($rs);
  }
 }
 private $upload_type = 1;
 /**
  * $rs['info'] = 'http://'.$_SERVER['HTTP_HOST'].substr($ok["path"],34);
  * 原图上传
  */
 private function moveUploadFile($file_path, $key)
 {
  if ($this->upload_type == 1) {
   $ok = @move_uploaded_file($file_path, $key);
   $result = [
       "code" => $ok,
       "path" => $key,
       "domain" => '',
       "bucket" => ''
   ];
   return $result;
  }
 }
 /**
  * 模块化首页
  * @return string
  */
 public function ModIndex()
 {
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id =$this-> getMchId($app_id);
  $data = [
      'mch_id'  => $mch_id,
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->mod_index($mch_id);
  if(empty($info)){
   $rs['code'] = 1;
   $rs['msg'] = '未选择首页模板';
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 public function power(){
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id =$this->getMchId($app_id);
  $id=Request::instance()->param('id');
  $data = [
      'mch_id'  => $mch_id,
      'id'  => $id,
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
      ['id','require','未选择万能页面'],
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->get_power($data);
  if(empty($info)){
   $rs['code'] = 1;
   $rs['msg'] = '请填充页面内容';
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 底部菜单
  * @return string
  */
 public function menu()
 {
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id =$this->getMchId($app_id);
  $data = [
      'mch_id'  => $mch_id,
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->get_menu($mch_id);
  if(empty($info)){
   $rs['code'] = 1;
   $rs['msg'] = '未选择底部菜单';
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 底部菜单
  * @return string
  */
 public function tabbar()
 {
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id =$this->getMchId($app_id);
  $data = [
      'mch_id'  => $mch_id,
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->get_tabbar($mch_id);
  if(empty($info)){
   $rs['code'] = 1;
   $rs['msg'] = '未选择底部菜单';
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 表单
  * @return string
  */
 public function form()
 {
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $id = Request::instance()->param('id');
  $mch_id =$this->getMchId($app_id);
  $data = [
      'mch_id'  => $mch_id,
      'id'=>$id
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
      ['id','require','未选择表单']
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->get_form($data);
  if(empty($info)){
   $rs['code'] = 1;
   $rs['msg'] = '表单不存在';
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 public function submitform(){
  $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $param = Request::instance()->param('data');
  $bus_form_id = Request::instance()->param('bus_form_id');
  $user_id = Request::instance()->param('user_id');
  $form=Request::instance()->param('form');
  $mch_id =$this->getMchId($app_id);
  $data = [
      'mch_id'  => $mch_id,
      'param'=>$param,
      'user_id'=>$user_id,
      'bus_form_id'=>$bus_form_id,
      'form'=>$form
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
      ['param','require','内容不能为空'],
      ['bus_form_id','require','表单不存在'],
      ['form','require','表单不存在'],
      ['user_id','require','未获取到用户信息，请重试']
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $limit_num=Db::name('ybsc_bus_form')->where('id',$bus_form_id)->value('limit_num');
  $num=Db::name('ybsc_user_form')->where(['mch_id'=>$mch_id,'bus_form_id'=>$bus_form_id,'user_id'=>$user_id])->count();
  if($limit_num!=0 && $num>=$limit_num){
   $rs['code'] = 1;
   $rs['msg'] = '提交次数已达到上线';
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->submit_form($data);
  if(empty($info)){
   $rs['code'] = 1;
   $rs['msg'] = '操作失败';
   return json_encode($rs);
  }
  if($info==-1){
   $rs['code'] = 1;
   $rs['msg'] = '已添加成功';
   return json_encode($rs);
  }
  $rs['info'] = $info;
  return json_encode($rs);
 }
 /**
  * 会员中心
  * @return string
  */
 public function ucenter()
 {
  // $rs = array('code' => 0,'info' => array());
  $app_id = Request::instance()->param('i');       //小程序id
  $mch_id =$this->getMchId($app_id);
  $data = [
      'mch_id'  => $mch_id,
  ];
  $rule = [
      ['mch_id','require','该商户不存在,请重新核对小程序配置'],
  ];
  $result = $this->checkParam($rule,$data);
  if(!empty($result)){
   $rs['code'] = 1;
   $rs['msg'] = $result;
   return json_encode($rs);
  }
  $index  = new IndexService();
  $info = $index->get_ucenter($mch_id);
  if(!empty($info)){
   $list=json_decode($info,true);
   if(empty($list['page']))
   {
    $list = array();
   }
   else
   {
    foreach($list['all_data'] as $k=>$v){
     if(isset($this->mem_path[$v['type']])){
      $list['all_data'][$k]['type']='caidan';
      $list['all_data'][$k]['path_url']=$this->mem_path[$v['type']];
      $list['all_data'][$k]['flag'] = $v['type'];
     }
    }
   }
  }
  if(empty($list))
  {
   $str = '{"all_data":[{"type":"member","time_key":"153813706361770","name":"\u4f1a\u5458\u4fe1\u606f"},{"type":"caidan","time_key":"153813706382566","name":"\u6211\u7684\u8ba2\u5355","path_url":"\/yb_shop\/pages\/order\/index","flag":"order"},{"type":"order_status","time_key":"153822855089529"},{"type":"caidan","time_key":"153813706454788","name":"\u6211\u7684\u5173\u6ce8","path_url":"\/yb_shop\/pages\/member\/favorite\/index","flag":"follow"},{"type":"caidan","time_key":"153813706433051","name":"\u6211\u7684\u4f18\u60e0\u5238","path_url":"\/yb_shop\/pages\/member\/coupon\/index","flag":"coupon"},{"type":"caidan","time_key":"153813706671813","name":"\u5728\u7ebf\u5ba2\u670d","path_url":"","flag":"kefu"},{"type":"caidan","time_key":"153813706663482","name":"\u6536\u8d27\u5730\u5740","path_url":"\/yb_shop\/pages\/member\/address\/index","flag":"address"},{"type":"caidan","time_key":"153813706794524","name":"\u5173\u4e8e\u6211\u4eec","path_url":"\/yb_shop\/pages\/member\/about\/index","flag":"about"}],"page":{"name":"\u4f1a\u5458\u4e2d\u5fc3","nv_color":"#f2f2f2","bg_color":"#f2f2f2","text_color":"#000000","bg_img":null,"open_img":null,"open_img_url":null,"open_img_name":null,"show_tabbar":"false"}}';
   return $str;
  }
  return json_encode($list);
 }
}